//
//  StudioConfiguration.h
//  AnotherMediaTool
//
//  Created by fh on 2018/9/13.
//  Copyright © 2018年 fh. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef struct  SCVideoCleanAperture {
    float width, height, horizontalOffset, verticalOffset;
} SCVideoCleanAperture;

static inline SCVideoCleanAperture SCVideoCleanApertureMake(float width, float height, float horizontalOffset, float verticalOffset) {
    SCVideoCleanAperture videoCleanAperture = {width, height, horizontalOffset, verticalOffset};
    return videoCleanAperture;
}

typedef struct SCVideoPixelAspectRatio {
    NSUInteger horizontalSpacing, verticalSpacing;
} SCVideoPixelAspectRatio;

static inline SCVideoPixelAspectRatio SCVideoPixelAspectRatioMake(NSUInteger horizontalSpacing, NSUInteger verticalSpacing) {
    SCVideoPixelAspectRatio videoPixelAspectRatio = {horizontalSpacing, verticalSpacing};
    return videoPixelAspectRatio;
}

// 视频编码类型
typedef NS_ENUM(NSInteger, SCVideoCodecType) {
    SCVideoCodecTypeH264,
    SCVideoCodecTypeJPEG,
};

// 对应AVVideoScalingModeKey
typedef NS_ENUM(NSInteger, SCVideoScalingMode) {
    SCVideoScalingModeFit,
    SCVideoScalingModeResize,
    SCVideoScalingModeResizeAspect,
    SCVideoScalingModeResizeAspectFill
};

// 视频压缩标准
typedef NS_ENUM(NSInteger, SCVideoProfileLevel) {
    SCVideoProfileLevelH264Baseline30,
    SCVideoProfileLevelH264Baseline31,
    SCVideoProfileLevelH264Baseline41,
    SCVideoProfileLevelH264BaselineAutoLevel,
    SCVideoProfileLevelH264Main30,
    SCVideoProfileLevelH264Main31,
    SCVideoProfileLevelH264Main32,
    SCVideoProfileLevelH264Main41,
    SCVideoProfileLevelH264MainAutoLevel,
    SCVideoProfileLevelH264High40,
    SCVideoProfileLevelH264High41,
    SCVideoProfileLevelH264HighAutoLevel
};

// 熵编码模式
typedef NS_ENUM(NSInteger, SCVideoH264EntropyMode) {
    SCVideoH264EntropyModeCAVLC = 0,
    SCVideoH264EntropyModeCABAC
};

// 分辨率
typedef NS_ENUM(NSInteger, SCCaptureSessionPreset) {
    SCCaptureSessionPresetPhoto = 0,
    SCCaptureSessionPresetHigh,
    SCCaptureSessionPresetMedium,
    SCCaptureSessionPresetLow,
    SCCaptureSessionPreset352x288,
    SCCaptureSessionPreset640x480,
    SCCaptureSessionPreset1280x720,
    SCCaptureSessionPreset1920x1080,
    SCCaptureSessionPreset3840x2160,
    SCCaptureSessionPresetiFrame960x540,
    SCCaptureSessionPresetiFrame1280x720,
    SCCaptureSessionPresetInputPriority
};

/**
 视频压缩参数设置方式

 - SCVideoConfigurationModeOutputSettingsAssistant: 使用系统类AVOutputSettingsAssistant快速设置压缩参数
 - SCVideoConfigurationModeCustom: 自定义视频压缩参数
 */
typedef NS_ENUM(NSInteger, SCVideoConfigurationMode) {
    SCVideoConfigurationModeOutputSettingsAssistant,
    SCVideoConfigurationModeCustom
};

// 系统简易设置视频压缩输出分辨率
typedef NS_ENUM(NSInteger, SCOutputSettingsPreset) {
    SCOutputSettingsPreset640x480,
    SCOutputSettingsPreset960x540,
    SCOutputSettingsPreset1280x720,
    SCOutputSettingsPreset1920x1080,
    SCOutputSettingsPreset3840x2160,
    SCOutputSettingsPresetHEVC1920x1080,
    SCOutputSettingsPresetHEVC3840x2160
};

/////////////////////
// 音频格式
typedef NS_ENUM(NSInteger, SCAudioFormat) {
    SCAudioFormatAAC,
    SCAudioFormatMP3
};

// 音频质量
typedef NS_ENUM(NSInteger, SCAudioEncoderAudioQuality) {
    SCAudioEncoderAudioQualityMin,
    SCAudioEncoderAudioQualityLow,
    SCAudioEncoderAudioQualityMedium,
    SCAudioEncoderAudioQualityHigh,
    SCAudioEncoderAudioQualityMax
};

// 比色法: unnecessary property
typedef NS_ENUM(NSInteger, SCVideoColorPropertiesMode) {
    SCVideoColorPropertiesModeHD,
    SCVideoColorPropertiesModeSD,
    SCVideoColorPropertiesModeWideGamutHD,
};

/**
 音视频编码配置类
 */
@interface StudioConfiguration : NSObject
/**************************************    视频压缩相关设置      ***************************************************************/
/** 设置视频压缩参数的方式:默认SCVideoConfigurationModeCustom */
@property (nonatomic, assign) SCVideoConfigurationMode videoConfigurationMode;
/** 视频压缩后的输出分辨率:该属性值只在videoConfigurationMode为SCVideoConfigurationModeOutputSettingsAssistant的时候才有效,使用SCVideoConfigurationModeOutputSettingsAssistant方式只需要设置该属性即可 */
@property (nonatomic, assign) SCOutputSettingsPreset videoOutputSettingsPreset;

/** 视频编码类型:AVVideoCodecH264/AVVideoCodecJPEG,iOS11支持AVVideoCodecTypeHEVC */
@property (nonatomic, assign) SCVideoCodecType videoCodecType;
/** 视频宽 */
@property (nonatomic, assign) NSUInteger videoWidth;
/** 视频高 */
@property (nonatomic, assign) NSUInteger videoHeight;
/** VVideoScalingModeKey:AVVideoScalingModeFit不支持,跟视频水平垂直比例和视频宽高有关 */
@property (nonatomic, assign) SCVideoScalingMode videoScalingMode;
/** 光圈区域设置: 使用SCVideoCleanApertureMake */
@property (nonatomic, assign) SCVideoCleanAperture videoCleanAperture;
/** 画面像素比例,设置后根据像素比居中按比例截取画面,水平:垂直默认1:1 ; 使用SCVideoPixelAspectRatioMake */
@property (nonatomic, assign) SCVideoPixelAspectRatio videoPixelAspectRatio;


// AVVideoCompressionPropertiesKey
/** 视频码率:bits/s,只支持H.264编码 */
@property (nonatomic, assign) NSUInteger videoAverageBitRate;
/** 帧速率:单位->帧/秒,即fps,高于30帧/秒设置该值 */
@property (nonatomic, assign) NSUInteger videoExpectedSourceFrameRate;
/** 视频压缩标准:见AVVideoSettings */
@property (nonatomic, assign) SCVideoProfileLevel videoProfileLevel;
/** 关键帧之间的最大间隔,也称为关键帧速率:1表示关键帧,只支持H.264编码 */
@property (nonatomic, assign) NSUInteger videoMaxKeyFrameInterval;
/** 从一个关键帧到下一个关键帧的最大持续时间，以秒为单位:默认为0,这意味着没有限制,只支持H.264编码 */
@property (nonatomic, assign) float videoMaxKeyFrameIntervalDuration;
/** 熵编码压缩方式:只支持H.264编码,谨慎使用该属性,一般不设置默认就行 */
@property (nonatomic, assign) SCVideoH264EntropyMode videoH264EntropyMode;
/** 启用或禁用帧重新排序:NSNumber (BOOL),默认YES,由编码器决定是否启用帧重排序,AVCaptureVideoDataOutput建议设置为NO */
@property (nonatomic, assign) BOOL videoAllowFrameReordering;
/** 为每秒的视频编码所需的非可丢弃帧的平均数量:单位->帧/秒,即fps */
@property (nonatomic, assign) NSUInteger videoAverageNonDroppableFrameRate;

/** 设置会话分辨率:设置完该属性通过属性preset获取到AVCaptureSession可用的分辨率 */
@property (nonatomic, assign) SCCaptureSessionPreset sessionPreset;
/** 会话分辨率 */
@property (nonatomic, readonly, strong) NSString *avCaptureSessionPreset;

/** 视频压缩配置,外部通过该值使用 */
@property (nonatomic, readonly, strong) NSMutableDictionary *videoOutputSettings;

/**
 默认压缩配置,非单例

 @return StudioConfiguration实例
 */
+ (instancetype)defaultConfiguration;

/** 单例配置:使用默认配置 */
+ (instancetype)shareInstanceConfiguration;

/**
 还原默认配置
 */
- (void)resetDefaultConfigurate;

/**************************************    音频压缩相关设置      ***************************************************************/

/** 录音格式 */
@property (nonatomic, assign) SCAudioFormat audioFormatID;
/** 录音通道数: 1 或 2 */
@property (nonatomic, assign) NSInteger audioNumberOfChannels;
/** 录音采样率(Hz): 如 AVSampleRateKey == 8000/44100/96000（影响音频的质量） */
@property (nonatomic, assign) float audioSampleRate;
/**  */
@property (nonatomic, assign) NSInteger audioEncoderBitRatePerChannel;
/** 录音质量 */
@property (nonatomic, assign) SCAudioEncoderAudioQuality audioEncoderAudioQuality;
/** 线性音频的位深度: 8、16、24、32 */
@property (nonatomic, assign) NSInteger audioLinearPCMBitDepth;
@property (nonatomic, strong) NSData *audioChannelLayout;
/** 音频压缩配置,外部通过该值使用 */
@property (nonatomic, readonly, strong) NSMutableDictionary *audioOutputSettings;




// AVVideoCompressionPropertiesKey unnecessary property
/** 光圈模式:有3种,见AVVideoSettings */
@property (nonatomic, strong) NSString *videoApertureMode;
/** 视频压缩质量:0.0-1.0,只支持JPEG和HEIC编码,1.0即无损压缩 */
@property (nonatomic, strong) NSNumber *videoQuality;


/** 广域颜色支持:NSNumber(BOOL),iOS10之后 */
@property (nonatomic, assign) NSNumber *videoAllowWideColor;
/** 比色法: iOS10之后 */
- (NSDictionary *)videoColorPropertiesWithSCVideoColorPropertiesMode:(SCVideoColorPropertiesMode)colorimetryMode;

@end






